<template>
  <section class="todoapp">
    <TodoHeader @addFn="babaAddFn" />
    <TodoMain :arr="showlist" @delFn="babaDelFn" />
    <TodoFooter
      v-if="list.length"
      :arr="list"
      @switch-todo="babaSwitchFn"
      @clearFn="babaClearFn"
    />
  </section>
</template>

<script>
import "./assets/css/base.css";
import "./assets/css/index.css";

import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
export default {
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        localStorage.setItem("list", JSON.stringify(newVal));
      },
    },
  },
  computed: {
    showlist() {
      if (this.babaType == "all") {
        return this.list;
      } else if (this.babaType == "no") {
        return this.list.filter((item) => item.isDone == false);
      } else {
        return this.list.filter((item) => item.isDone == true);
      }
    },
  },
  methods: {
    babaAddFn(name) {
      const obj = {
        id: +new Date(),
        name,
        isDone: false,
      };
      this.list.push(obj);
    },
    babaDelFn(i) {
      this.list.splice(i, 1);
    },
    babaSwitchFn(switchType) {
      this.babaType = switchType;
    },
    babaClearFn() {
      this.list = this.list.filter((item) => item.isDone == false);
    },
  },
  data() {
    return {
      babaType: "all",
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
};
</script>

<style>
</style>